/**
 *
 */
package com.honeybees.web.captcha.action;

import java.io.IOException;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.honeybees.framework.common.util.HTTPUtils;
import com.honeybees.framework.spring.mvc.WebController;
import com.honeybees.framework.spring.mvc.model.ReturnMessage;
import com.honeybees.web.captcha.cache.CaptchaCacheHandler;
import com.honeybees.web.captcha.service.CaptchaService;

/**
 * <dl>
 * <dt><b> 验证码 </b></dt>
 * <p>
 * <dd>功能描述</dd>
 * </dl>
 * <p>
 * Copyright (C) All rights reserved.
 * </p>
 *
 * @author 李远明
 * @version 2016-05-27 11:19:13 移到com.honeybees.web.captcha.controller包下
 * @since 2016-01-31 15:34:28
 */
@RequestMapping(value = "public/captcha")
@Controller
public class CaptchaAction extends WebController<ReturnMessage> {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Resource
    private CaptchaService service;

    @Resource
    private CaptchaCacheHandler cacheHandler;

    /**
     * <dl>
     * <dt><b> 获取 验证码 图片 </b></dt>
     * <p>
     * <dd>功能描述</dd>
     * </dl>
     *
     * @param request  当前请求
     * @param response 当前响应
     * @author 李远明
     * @version 2016-01-31 15:48:22
     * @version 2016-05-27 11:20:28 完成代码
     */
    // http://localhost:8080/sysmgmt/public/captcha/image?_=1488853586806
    @RequestMapping(value = "/image")
    public void getCaptchaImage(HttpServletRequest request, HttpServletResponse response) {
        HTTPUtils.setNoCacheHeader(response);
        response.setContentType(MediaType.IMAGE_PNG_VALUE);

        //String sessionId = SessionUtils.getSessionId();
        String sessionId = super.getSessionId(request);
        try {
            this.service.createCaptchaImage(sessionId, response.getOutputStream());
        } catch (IOException e) {
            this.logger.error("");
        } catch (Exception e) {
        }
    }

    /**
     * <dl>
     * <dt><b> 校验验证码 </b></dt>
     * <p>
     * <dd>功能描述</dd>
     * </dl>
     *
     * @param request 当前请求
     * @param code    验证码
     * @author 李远明
     * @version 2016-05-27 13:43:25
     */
    // http://localhost:8080/sysmgmt/public/captcha/verify?code=pbaw
    @RequestMapping(value = "/verify", method = RequestMethod.POST)
    @ResponseBody
    public ReturnMessage verifyCaptcha(HttpServletRequest request, String code) {
        if (StringUtils.isBlank(code)) {
            returnMsg = FAIL_MESSAGE;
            returnMsg.setMessage("请输入验证码…");
            return returnMsg;
        }
        String sessionId = super.getSessionId(request);
        String captchaCode = this.cacheHandler.getCaptchaCode(sessionId);

        boolean eq = code.equalsIgnoreCase(captchaCode);
        returnMsg.setMessage(eq);
        return returnMsg;
    }

    /**
     * <dl>
     * <dt><b> 校验验证码 </b></dt>
     * <p>
     * <dd>用于 jquery-validation "remote" 方式校验 </dd>
     * </dl>
     *
     * @param request     当前请求
     * @param captchaCode 验证码
     * @author 李远明
     * @version 2017-04-06 17:23
     */
    @RequestMapping(value = "/validate", method = RequestMethod.GET)
    @ResponseBody
    public boolean validateCaptcha(HttpServletRequest request, String captchaCode) {
        if (captchaCode == null || captchaCode.trim().isEmpty()) {
            return false;
        }
        String sessionId = super.getSessionId(request);
        String cacheCaptchaCode = this.cacheHandler.getCaptchaCode(sessionId);
        if (cacheCaptchaCode == null || cacheCaptchaCode.trim().isEmpty()) {
            return false;
        }
        return cacheCaptchaCode.equalsIgnoreCase(captchaCode);
    }

}
